package com.mzbopen.rightbottommoreview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


public class LineToTriangleView extends View {
    private Paint paint = new Paint();
    float newLocation = 1;

    private int line_color = Color.BLACK;
    private int paint_width = 0;

    public LineToTriangleView(@NonNull Context context) {
        super(context);
        init(context, null);

    }

    public LineToTriangleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public LineToTriangleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {


        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.line_to_triangle_view);
            paint_width = ta.getDimensionPixelSize(R.styleable.line_to_triangle_view_line_width, ScreenUtil.dp2px(context, 3));
            line_color = ta.getColor(R.styleable.line_to_triangle_view_line_color, Color.BLACK);

            paint = new Paint();
            paint.setStrokeWidth(paint_width);
            //设置画笔颜色
            paint.setColor(line_color);
            //STROKE                //描边ScreenUtil
            //FILL                  //填充
            //FILL_AND_STROKE       //描边加填充
            //设置画笔模式
            paint.setStyle(Paint.Style.FILL);

            ta.recycle();

        }

    }

    private void initPaint(Context context, AttributeSet attrs) {

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        float width = getWidth();
        float center_cheap = width / 3;

        float location_width = ((width - paint_width) / 2.0f * (1 - newLocation)) - (width - paint_width) / 2.0f;
        if (location_width < paint_width) location_width = paint_width;
        canvas.drawLine(center_cheap + (width - paint_width) / 2.0f, paint_width / 2.0f, center_cheap + location_width, height / 2.0f, paint);
        canvas.drawLine(center_cheap + location_width, height / 2.0f, center_cheap + (width - paint_width) / 2.0f, height - paint_width / 2.0f, paint);
        canvas.drawCircle(center_cheap + location_width, height / 2.0f, paint_width / 2.0f, paint);

        canvas.drawCircle(center_cheap + (width - paint_width) / 2.0f, paint_width / 2.0f, paint_width / 2.0f, paint);

        canvas.drawCircle(center_cheap + (width - paint_width) / 2.0f, height - paint_width / 2.0f, paint_width / 2.0f, paint);


    }

    public void setNewLocation(float newLocation) {
        if (newLocation < -1) newLocation = -1;
        this.newLocation = newLocation;
        invalidate();


    }
}
